package com.corelibs.expand

import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import com.lib.core.utils.ToastMgr


fun showToast(msg:String) {
    ToastMgr.show(msg)
}

fun showToast(msg: Int) {
    ToastMgr.show(msg)
}


/**
 * @param root 最外层布局，需要调整的布局
 * @param scrollToView 被键盘遮挡的scrollToView，滚动root,使scrollToView在root可视区域的底部
 */
 fun controlKeyboardLayout(root: View, scrollToView: View) {
    root.viewTreeObserver.addOnGlobalLayoutListener {
        val rect = Rect()
        //获取root在窗体的可视区域
        root.getWindowVisibleDisplayFrame(rect)
        //获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
        val rootInvisibleHeight: Int = root.rootView.height - rect.bottom
        //若不可视区域高度大于100，则键盘显示
        if (rootInvisibleHeight > 100) {
            val location = IntArray(2)
            //获取scrollToView在窗体的坐标
            scrollToView.getLocationInWindow(location)
            //计算root滚动高度，使scrollToView在可见区域
            val srollHeight: Int = location[1] + scrollToView.height - rect.bottom
            root.scrollTo(0, srollHeight)
        } else {
            //键盘隐藏
            root.scrollTo(0, 0)
        }
    }
}


